package com.hejia.alauda.jms;

import com.hejia.alauda.entity.User;
import com.hejia.alauda.service.MessageService;
import com.hejia.alauda.service.UserService;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import javax.annotation.Resource;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.text.SimpleDateFormat;

/**
 * 
 * 注册消息监听器
 * 
 *
 *
 * Created by showone on 2014 上午10:07:20.
 *
 * @Copyright: 2014 www.hejiajinrong.com All rights reserved.
 */
public class RegisterMessageListener implements MessageListener {

	private static final Log Logger = LogFactory
			.getLog(RegisterMessageListener.class);

	@Resource
	private MessageService messageService;

	@Resource
	private UserService userService;

	@Override
	public void onMessage(Message message) {

		try {
			Logger.info("接收用户注册消息 " + message);

			if (!(message instanceof ActiveMQTextMessage)) {
				return;
			}

			ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message;

			ObjectMapper mapper = new ObjectMapper();

			JsonNode jsonNode = mapper.readTree(textMessage.getText());
			String id = jsonNode.get("id").asText();
			User user = this.userService.findUserById(Long.parseLong(id));
			if (user == null) {
				Logger.warn("找不到注册用户，用户id：" + id);
				return;
			}

		} catch (Exception e) {
			Logger.error("销售结束产品入库执行失败", e);
		}
	}
}
